// $codepro.audit.disable com.instantiations.assist.eclipse.analysis.unusedReturnValue
/**
 * 
 */
package edu.gatech.utilities;

import java.io.IOException;
import java.io.Serializable;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;

/**
 * Connects to a PRISM server and SCP's the file containing
 * all of the Serialized objects.
 * @author Dan Holdridge
 * @version 1.0
 *
 */
public class Server implements Serializable{

	/**
	 * Needed for Serializable objects
	 */
	private static final long serialVersionUID = 6565204875434430952L;

	/**
	 * The username to connect to the SCP server
	 */
	private static final String SERVER_USERNAME = "dholdridge3";

	/**
	 * Password needed to connect
	 */
	private static final String SERVER_PASSWORD = "LOLtemppassword1";

	/**
	 * The hostname of the server being connected to
	 */
	private static final String SERVER_HOSTNAME = "acme.gatech.edu";

	/**
	 * Name of the file stored on the server
	 */
	private static final String REMOTE_FILE = "persistFile";

	/**
	 * Name of directory the file is saved to locally
	 */
	private static final String TARGET = "persist";

	/**
	 * Directory and name of the file to be uploaded
	 */
	private static final String LOCAL_FILE = "persist/persistFile";

	/**
	 * Name of directory on server the uploaded file will be stored
	 */
	private static final String REMOTE_TARGET_DIRECTORY = "";
	
	/**
	 * Connects to the server and retrieves the persistence file
	 */
	public static void get(){
		final Connection prism = new Connection(SERVER_HOSTNAME);
		try {
			prism.connect();
			prism.authenticateWithPassword(SERVER_USERNAME, SERVER_PASSWORD);
			final SCPClient fileTransfer = new SCPClient(prism);
			fileTransfer.get(REMOTE_FILE, TARGET);
		} catch (IOException e) {
			System.out.println("Error: could not connect to host");
			e.printStackTrace();
		} finally {
			prism.close();
		}
	}
	
	/**
	 * Connects to the server and retrieves the persistence file
	 */
	public static void put(){
		final Connection prism = new Connection(SERVER_HOSTNAME);
		try {
			prism.connect();
			prism.authenticateWithPassword(SERVER_USERNAME, SERVER_PASSWORD);
			final SCPClient fileTransfer = new SCPClient(prism);
			fileTransfer.put(LOCAL_FILE, REMOTE_TARGET_DIRECTORY);
		} catch(IOException e) {
			System.out.println("Error: could not connect to host");
			e.printStackTrace();
		} finally {
			prism.close();
		}
	}
	
	/**
	 * (non-Javadoc)
	
	 * @return String
	 * @see java.lang.Object#toString() 
	 * */
	public String toString(){
		return "SCP: " + SERVER_USERNAME + " connceted to " + SERVER_HOSTNAME;
	}
}
